package com.dn.flutter_toast;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import com.dormmom.flutter_stripe_payment.FlutterStripeFactory;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
 * FlutterToastPlugin
 */
public class FlutterToastPlugin implements MethodCallHandler {
    private static final String PLUGIN_NAME = "io/flutter_stripe_payment";
    private static Activity currentActivity;
    private static BinaryMessenger messenger;

    /**
     * Plugin registration.
     */
    public static void registerWith(Registrar registrar) {
        Log.d("FlutterToastPlugin", "-------------------registrar: PluginRegistry.Registrar");
        currentActivity = registrar.activity();
        messenger = registrar.messenger();
//        FlutterStripePaymentPlugin.setUpPluginMethods(registrar.activity(), registrar.messenger(), currentActivity);
        new MethodChannel(messenger, PLUGIN_NAME).setMethodCallHandler(new MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull @NotNull MethodCall call, @NonNull @NotNull Result result) {
                Map<String, Object> arguments = new HashMap<>();
                if (call.arguments != null) {
                    arguments = (Map<String, Object>) call.arguments;
                }
                String clientSecret = "";
                String paymentMethodId = "";
                FlutterStripeFactory flutterStripeFactory = new FlutterStripeFactory(currentActivity, messenger);
                Log.d("FlutterToastPlugin", "-------------------call.method："+call.method);
                switch (call.method) {

                    case "getPlatformVersion":
                        result.success("Android ${android.os.Build.VERSION.RELEASE}");
                        break;

                    case "setStripeSettings":
                        String stripePublishableKey = (String) arguments.get("stripePublishableKey");
                        flutterStripeFactory.setStripeSettings(result, stripePublishableKey);

                        break;
                    case "addPaymentMethod":
                        flutterStripeFactory.addPaymentMethod(result);
                        break;
                    case "confirmPaymentIntent":
                        clientSecret = (String) arguments.get("clientSecret");
                        paymentMethodId = (String) arguments.get("paymentMethodId");
                        String stripeAccountId = (String) arguments.get("stripeAccountId");
                        Double amount = (Double) arguments.get("amount");
                        flutterStripeFactory.confirmPaymentIntent(paymentMethodId, clientSecret, amount, result);
                        break;
                    case "setupPaymentIntent":
                        clientSecret = (String) arguments.get("clientSecret");
                        paymentMethodId = (String) arguments.get("paymentMethodId");
                        if (paymentMethodId != null && clientSecret != null) {
                            flutterStripeFactory.setupPaymentIntent(paymentMethodId, clientSecret, result);
                        }
                        break;
                    default:
                        result.notImplemented();
                        break;
                }

            }
        });

    }

    @Override
    public void onMethodCall(MethodCall call, Result result) {
    }
}
